package com.bw.util;


import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 *  JDBC工具类
 */
public class JDBCUtil {


    //获得数据库连接
    public  static Connection  getConn() throws Exception {

        //读取配置文件
        Properties pp = new Properties();

        InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        //加载
        pp.load(is);
        String driver = pp.getProperty("driver");//com.mysql.jdbc.Driver
        String url = pp.getProperty("url");
        String user = pp.getProperty("username");
        String password = pp.getProperty("password");

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);

        return conn;
    }


    //② 关闭数据库连接
    public  static  void  close(ResultSet rs, PreparedStatement ps, Connection conn) throws Exception {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }
    }




}
